/**
 * Created by ruiyuan on 15-9-23.
 */
'use strict';


var shareApp = angular.module('shareApp', ['ngRoute', 'ngResource']);

shareApp.
    controller('ShareActivityDetailCtrl', ['$scope', '$http', '$location', function ($scope, $http, $location) {
        $http.get(serverUrl + '/rest/v1.0/mobile/shares/getCommunityActivity/' + params.activityId + '/' + params.token + '/' + params.deviceId, {
            headers: {'Content-Type': 'application/json;charset=utf-8'}
        }).success(function (data) {
            $scope.activity = data.data;
            if (data.data.isPartake == true) {
                $('.join-a').addClass('joined-a').html('已报名');
            }
            var str = "";
            for (var i = 0; i < data.data.images.length; i++) {
                str += '<div class="swiper-slide"><img src="' + data.data.images[i].imageURL + '"> </div>';
            }
            $('.swiper-wrapper').html(str);
            //轮播图片
            var mySwiper = new Swiper('.swiper-container', {
                pagination: '.pagination',
                paginationClickable: true
            });
        });

        $scope.partake = function () {
            var opt = {
                phone: $scope.phone,
                desc: $scope.desc
            }
            $http.post('http://' + serverUrl + '/rest/v1.0/activity/partakeCommunityActivity/' + params.activityId + '/' + params.token + '/' + params.deviceId, opt, {
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function (data) {
            });
        }
    }]).
    controller('ActivityDetailCtrl', ['$scope', '$http', 'ActivityDetail', '$location', function ($scope, $http, ActivityDetail, $location) {
        $http.get( serverUrl + '/rest/v1.0/activity/getCommunityActivity/' + params.activityId + '/' + params.token + '/' + params.deviceId, {
            headers: {'Content-Type': 'application/json;charset=utf-8'}
        }).success(function (data) {
            $scope.activity = data.data;
            if (data.data.isPartake == true) {
                $('.join-a').addClass('joined-a').html('已报名');
            }
            var str = "";
            for (var i = 0; i < data.data.images.length; i++) {
                str += '<div class="swiper-slide"><img src="' + data.data.images[i].imageURL + '"> </div>';
            }
            $('.swiper-wrapper').html(str);
            var mySwiper = new Swiper('.swiper-container', {
                pagination: '.pagination',
                paginationClickable: true
            });
        });

        $scope.partake = function () {
            var opt = {
                phone: $scope.phone,
                desc: $scope.desc
            }
            $http.post(serverUrl + '/rest/v1.0/activity/partakeCommunityActivity/' + params.activityId + '/' + params.token + '/' + params.deviceId, opt, {
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function (data) {
            });
        }
    }]);


var params = {};
var tempArray = window.location.search.split('?');
if (tempArray.length == 2) {
    var parts = tempArray[1].split('&');
    for (var i = 0; i < parts.length; i++) {
        var nv = parts[i].split('=');
        if (!nv[0]) continue;
        params[nv[0]] = nv[1] || true;
    }
}

shareApp.factory('GoodsDetail', ['$resource',
    function ($resource) {
        //请求接口
        return $resource(serverUrl + '/rest/v1.0/market/getGoodsDetail/' + params.goodsId + '/' + params.token + '/' + params.deviceId,
            {}
        );
    }]);

shareApp.factory('ReplyList', ['$resource',
    function ($resource) {
        //请求接口
        return $resource(serverUrl + '/rest/v1.0/market/getRepliesByGoodsId/' + params.goodsId + '/' + params.token + '/' + params.deviceId,
            {}
        );
    }])

shareApp.factory('Reply', ['$resource',
    function ($resource) {
        //请求接口
        return $resource(serverUrl + '/rest/v1.0/market/addReply/' + params.token + '/' + params.deviceId,
            {}
        );
    }]);

shareApp.factory('ActivityDetail', ['$resource',
    function ($resource) {
        //请求接口
        return $resource(serverUrl + '/rest/v1.0/activity/getCommunityActivity/' + params.activityId + '/' + params.token + '/' + params.deviceId,
            {}
        );
    }]);

function formatTime(input) {

    var d = new Date(input), now = new Date(),
        dStamp = d.getTime(), nowStamp = now.getTime();
    if (now.getFullYear() - d.getFullYear() > 0) {
        return d.getFullYear() + "-" + (d.getMonth() + 1) + "-" + d.getDate();
    } else if ((nowStamp - dStamp) / 1000 < 60) {
        return "刚刚";
    } else if ((nowStamp - dStamp) / 60000 < 60) {
        return Math.floor((nowStamp - dStamp) / 60000) + "分钟前";
    } else if ((nowStamp - dStamp) / 3600000 < 24) {
        return Math.floor((nowStamp - dStamp) / 3600000) + "小时前";
    } else if ((nowStamp - dStamp) / 86400000 >= 1 && (nowStamp - dStamp) / 86400000 <= 3) {
        return Math.floor((nowStamp - dStamp) / 86400000) + "天前";
    } else if ((nowStamp - dStamp) / 86400000 > 3) {
        return (d.getMonth() + 1) + "月" + d.getDate() + "日";
    }
};